package com.erp.app.catalog.itemMaster.dao;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.erp.app.catalog.itemMaster.pk.ItemMasterDtlPk;
import com.erp.app.catalog.itemMaster.vo.ItemMasterDtlVO;
//import com.erp.app.catalog.codeSetup.vo.CodeDtlVo;
import com.erp.app.setup.codeSetup.vo.CodeHdrVo;
import com.erp.framework.exceptions.FindRecordException;
import com.erp.framework.model.dao.SimpleSearchMaintStyleDao;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.model.vo.SimpleBaseVo;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;

public class ItemMasterDtlDao extends SimpleSearchMaintStyleDao{

	public ChildList selectByParent(SimpleBaseVo parent, Connection conn) throws FindRecordException {
		return super.selectByParent(parent, CodeHdrVo.class, conn);
	}

	protected String sqlInsert() {
		// TODO Auto-generated method stub
		return null;
	}

	protected void setValuesForInsert(PreparedStatement pStmt, Object obj) throws SQLException {
		// TODO Auto-generated method stub
		
	}

	protected String sqlUpdate() {
		// TODO Auto-generated method stub
		return null;
	}

	protected void setValuesForModify(PreparedStatement pStmt, Object obj) throws SQLException {
		// TODO Auto-generated method stub
		
	}

	protected String sqlDelete() {
		// TODO Auto-generated method stub
		return null;
	}

	protected void setValuesForDelete(PreparedStatement pStmt, Object obj) throws SQLException {
		// TODO Auto-generated method stub
		
	}

	protected String sqlFind() {
		String sql = " SELECT DISTINCT itme_dtl.ITEM_ID, itme_dtl.ITEM_DETAIL_ID, "
			+ " itme_dtl.ITEM_DETAIL_UNIT_OF_MEASURE, itme_dtl.ITEM_RECURSIVE_LINE_SEQUENCE, "
			+ " itme_dtl.ITEM_DETAIL_TYPE, itme_dtl.SEQUENCE_ORDER, itme_dtl.AUDIT_ID, "
			+ " itme_dtl.HIDDEN_FIELD, itme_dtl.ACTIVE, "
			+ " DECODE(NVL(itme_dtl.ITEM_RECURSIVE_LINE_SEQUENCE,-1),-1,itme_dtl.SEQUENCE_NO, "
			+ " SF_GET_RECDTL_ID(itme_dtl.ITEM_ID,itme_dtl.SEQUENCE_NO)) SEQUENCE_NO ,"
			+ " DECODE(NVL(itme_dtl.ITEM_RECURSIVE_LINE_SEQUENCE,- 1), "
			+ "- 1,DECODE(itme_dtl.ITEM_DETAIL_TYPE,'W',NVL((SELECT REF_DESC " 
            + " FROM   T_OPTION_CODE_DTL WHERE  COMPANY_CODE = 'KS' " 
            + " AND REF_MASTER_CODE = itme_dtl.ITEM_DETAIL_OPTION_CODE " 
            + " AND REF_CODE = itme_dtl.ITEM_DETAIL_VALUE),''),itme_dtl.ITEM_DETAIL_VALUE)," 
            + " SF_GET_RECDTL_DESC(itme_dtl.ITEM_ID,itme_dtl.SEQUENCE_NO)) " 
            + " ||DECODE(NVL(itme_dtl.ITEM_DETAIL_UNIT_OF_MEASURE,'-1'),'-1',' ', " 
            + " DECODE(NVL(itme_dtl.ITEM_DETAIL_VALUE,'-1'),'-1','',(SELECT ' '||code_dtl.REF_DESC " 
            + " FROM T_CODE_DTL code_dtl WHERE  code_dtl.COMPANY_CODE = 'KS' " 
	        + " AND code_dtl.REF_MASTER_CODE = 'UOM' " 
	        + " AND code_dtl.REF_CODE = itme_dtl.ITEM_DETAIL_UNIT_OF_MEASURE))) DTL_VALUES "
	        + " FROM T_ITEM_DETAIL itme_dtl WHERE itme_dtl.ITEM_ID = ? ";
		
		Logger.log(sql);
		return sql;
	}

	protected void setValuesForFind(PreparedStatement pStmt, Object obj) throws SQLException {
		ItemMasterDtlPk pk = (ItemMasterDtlPk)obj;
		int i = 1;
		pStmt.setBigDecimal(i++, new BigDecimal(pk.getItemId()));		
	}

	protected String sqlList() {
		String sql = " SELECT DISTINCT itme_dtl.ITEM_ID, itme_dtl.ITEM_DETAIL_ID, "
			+ " itme_dtl.ITEM_DETAIL_UNIT_OF_MEASURE, itme_dtl.ITEM_RECURSIVE_LINE_SEQUENCE, "
			+ " itme_dtl.ITEM_DETAIL_TYPE, itme_dtl.SEQUENCE_ORDER, itme_dtl.AUDIT_ID, "
			+ " itme_dtl.HIDDEN_FIELD, itme_dtl.ACTIVE, "
			+ " DECODE(NVL(itme_dtl.ITEM_RECURSIVE_LINE_SEQUENCE,-1),-1,itme_dtl.SEQUENCE_NO, "
			+ " SF_GET_RECDTL_ID(itme_dtl.ITEM_ID,itme_dtl.SEQUENCE_NO)) SEQUENCE_NO ,"
			+ " DECODE(NVL(itme_dtl.ITEM_RECURSIVE_LINE_SEQUENCE,- 1), "
			+ "- 1,DECODE(itme_dtl.ITEM_DETAIL_TYPE,'W',NVL((SELECT REF_DESC " 
            + " FROM   T_OPTION_CODE_DTL WHERE  COMPANY_CODE = 'KS' " 
            + " AND REF_MASTER_CODE = itme_dtl.ITEM_DETAIL_OPTION_CODE " 
            + " AND REF_CODE = itme_dtl.ITEM_DETAIL_VALUE),''),itme_dtl.ITEM_DETAIL_VALUE)," 
            + " SF_GET_RECDTL_DESC(itme_dtl.ITEM_ID,itme_dtl.SEQUENCE_NO)) " 
            + " ||DECODE(NVL(itme_dtl.ITEM_DETAIL_UNIT_OF_MEASURE,'-1'),'-1',' ', " 
            + " DECODE(NVL(itme_dtl.ITEM_DETAIL_VALUE,'-1'),'-1','',(SELECT ' '||code_dtl.REF_DESC " 
            + " FROM T_CODE_DTL code_dtl WHERE  code_dtl.COMPANY_CODE = 'KS' " 
	        + " AND code_dtl.REF_MASTER_CODE = 'UOM' " 
	        + " AND code_dtl.REF_CODE = itme_dtl.ITEM_DETAIL_UNIT_OF_MEASURE))) DTL_VALUES "
	        + " FROM T_ITEM_DETAIL itme_dtl WHERE itme_dtl.ITEM_ID = ? ";
		
		Logger.log(sql);
		return sql;
	}

	protected void setValuesForList(PreparedStatement pStmt, Object obj) throws SQLException {
		ItemMasterDtlPk pk = (ItemMasterDtlPk)obj;
		int i = 1;
		pStmt.setBigDecimal(i++, new BigDecimal(pk.getItemId()));		
	}

	protected SimpleBaseVo getResultSetValues(ResultSet rs) throws SQLException {
		ItemMasterDtlVO vo = new ItemMasterDtlVO();	
		vo.setItemId(CommonUtil.isEmpty(rs.getBigDecimal("ITEM_ID"))?"":rs.getBigDecimal("ITEM_ID").toString());
		vo.setItemDtlId(CommonUtil.isEmpty(rs.getBigDecimal("ITEM_DETAIL_ID"))?"":rs.getBigDecimal("ITEM_DETAIL_ID").toString());
		vo.setItemDtlUnitMeasure(rs.getString("ITEM_DETAIL_UNIT_OF_MEASURE"));
		vo.setItemRecursiveLineSequence((CommonUtil
				.isEmpty(rs.getBigDecimal("ITEM_RECURSIVE_LINE_SEQUENCE")))?"":rs.getBigDecimal("ITEM_RECURSIVE_LINE_SEQUENCE").toString());
		vo.setItemDtlType(rs.getString("ITEM_DETAIL_TYPE"));
		vo.setSeqOrder(CommonUtil.isEmpty(rs.getBigDecimal("SEQUENCE_ORDER"))?"":rs.getBigDecimal("SEQUENCE_ORDER").toString());
		vo.setAuditId(rs.getString("AUDIT_ID"));
		vo.setHiddenField(rs.getString("HIDDEN_FIELD"));
		vo.setActive(rs.getString("ACTIVE"));
		vo.setSeqNo(CommonUtil.isEmpty(rs.getBigDecimal("SEQUENCE_NO"))?"":rs.getBigDecimal("SEQUENCE_NO").toString());
		vo.setDtlValues(rs.getString("DTL_VALUES"));
		return vo;
	}

}
